package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.example.administrator.projectmoudle_two.R;

import static com.example.administrator.projectmoudle_two.R.id.btn;

/**
 * Created by eyesee on 2017/5/27.
 */

public class PopuwindowFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private View mView;
    private Button mBtn;
    private PopupWindow mPopup;
    private String[] datas = {"选项1", "选项2", "选项3", "选项4", "选项5"};
    private ListView mListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.populayout, null);
        initView(mView);
        return mView;
    }

    private void initView(final View view) {
        mBtn = ((Button) view.findViewById(btn));
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPopu();
                mPopup.showAsDropDown(mBtn,0,50);
            }
        });
    }

    private void initPopu() {
        View view = mContext.getLayoutInflater().inflate(R.layout.popuwindowlayout, null);

        //listview填充数据
        mListView = ((ListView) view.findViewById(R.id.pop_listview));
        mListView.setAdapter(new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,datas));
        mListView.setOnItemClickListener(this);

        mPopup = new PopupWindow(view,200,300);         //创建pop对象,指定宽高
        mPopup.setAnimationStyle(R.style.dialogTheme);  //设置动画
        mPopup.setFocusable(true);                      //设置焦点
        mPopup.setOutsideTouchable(true);               //设置窗口外点击取消
        mPopup.update();                                //更新pop状态
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String data = datas[position];
        Toast.makeText(mContext, data, Toast.LENGTH_SHORT).show();
        mPopup.dismiss();
    }
}
